package com.shtel.paas.monitorcenter.view.dispaly.common.ztree.service.impl;

import com.shtel.paas.monitorcenter.view.dispaly.common.ztree.mapper.QueryViewDisplayZtreeMapper;
import com.shtel.paas.monitorcenter.view.dispaly.common.ztree.service.QueryViewDisplayZtreeService;
import com.shtel.paas.monitorcenter.view.dispaly.common.ztree.vo.ViewDisplayZtreeListVO;
import com.shtel.paas.monitorcenter.view.dispaly.common.ztree.vo.ViewDisplayZtreeVO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version V1.0
 * @Description: 视图显示模块ztree service接口实现类
 * @author: zhangyy
 * @date: 2018年7月10日 上午9:03:31
 */
@Service
public class QueryViewDisplayZtreeServiceImpl implements QueryViewDisplayZtreeService {
    @Autowired
    QueryViewDisplayZtreeMapper queryViewDisplayZtreeMapper;

    //视图展示模块ztree公共查询方法接口
    @Override
    public Map<String, Object> queryViewDisplayZtree(String monitorCenterHostInfomationType) {
        Map typeMap=new HashMap();
        typeMap.put("0","业务系统");
        typeMap.put("1","分布式数据库");
        typeMap.put("2","消息中间件");
        typeMap.put("3","分布式缓存");
        typeMap.put("4","oracle");
        // 参数调整
        List<String> emp = new ArrayList<>();
        if (monitorCenterHostInfomationType.contains(",")) {  //逗号隔开说明有多个参数
            String[] ids = monitorCenterHostInfomationType.split(",");
            for (int i = 0; i < ids.length; i++) {
                if (ids[i] != null && ids[i] != "") {
                    emp.add(ids[i]);
                }
            }
        }else{
            emp.add(monitorCenterHostInfomationType);
        }

        // 从数据库取出的根级数据
        List<ViewDisplayZtreeListVO> resultArray = new ArrayList<>();
        resultArray = queryViewDisplayZtreeMapper.queryViewDisplayZtree(emp);
        //tree数据改造
        for(ViewDisplayZtreeListVO viewDisplayZtreeListVO:resultArray){
            viewDisplayZtreeListVO.setIsParent(true);
            List<ViewDisplayZtreeVO> childendListTemp =getTreeData(viewDisplayZtreeListVO.getName());
            viewDisplayZtreeListVO.setChildren(childendListTemp);
        }

        Map<String, Object> results = new HashMap<String, Object>();
        results.put("name", typeMap.get(monitorCenterHostInfomationType));
        results.put("children", resultArray);
        results.put("isParent", true);
        return results;
    }

    /**
     * 视图展示模块ztree子节点
     * creat by:zhangyy
     * @param
     * @return list
     */
    @Override
    public  List<ViewDisplayZtreeVO>  getTreeData(String id) {
        List<ViewDisplayZtreeVO> list= queryViewDisplayZtreeMapper.getTreeData(id);
        //tree数据改造
        for(ViewDisplayZtreeVO viewDisplayZtreeVO:list){
            viewDisplayZtreeVO.setIsParent("false");
        }
        return list;
    }
}
